﻿Imports Microsoft.VisualBasic
Imports System
Imports System.Collections 
Imports System.Data 
Imports System.Web.UI.WebControls 

Public Delegate Function MustAddARowHandler(ByVal data As IEnumerable) As IEnumerable

Public Class EmptyGridView
    Inherits GridView

    'Flag used to identify if the datasource is empty. 
    Private _isEmpty As Boolean = False
    Private MustAddARow As MustAddARowHandler

    Protected Overloads Overrides Sub OnDataBound(ByVal e As EventArgs)

        'if in DesignMode, don't do anything special. Just call base and return. 
        If DesignMode Then
            MyBase.OnDataBound(e)
            Exit Sub
        End If

        'hide the dummy row. 
        If _isEmpty Then
            Rows(0).Visible = False
        End If
        MyBase.OnDataBound(e)
    End Sub

    Protected Overloads Overrides Sub PerformDataBinding(ByVal data As IEnumerable)

        'If in DesignMode, don't do anything special. Just call base and return. 
        If DesignMode Then
            MyBase.PerformDataBinding(data)
            Exit Sub
        End If

        Dim objectItemCount As Integer = 0
        If data.[GetType]() Is GetType(DataView) Then
            objectItemCount = CType(data, DataView).Count
        Else
            'Count the data items.(I wish I knew a better way to do this.) 
            For Each o As Object In data
                objectItemCount += 1
            Next
        End If

        'If there is a count, don't do anything special. Just call base and return. 

        If objectItemCount > 0 Then
            MyBase.PerformDataBinding(data)
            Exit Sub
        End If

        'HttpContext.Current.Response.Write("53")
        'HttpContext.Current.Response.Write(data.[GetType]() Is GetType(DataView))
        'HttpContext.Current.Response.End()

        'Set these values so the GridView knows what's up. 
        SelectArguments.TotalRowCount += 1
        _isEmpty = True

        'If it's a DataView, it will work without having to handle the MustAddARow event. 
        If data.[GetType]() Is GetType(DataView) Then
            'Add a row and use that new view. 
            Dim dv As DataView = DirectCast(data, DataView)
            dv.Table.Rows.Clear()
            dv.Table.Rows.InsertAt(dv.Table.NewRow(), 0)
            MyBase.PerformDataBinding(dv.Table.DefaultView)
            Exit Sub
        Else
            'If you are using some custom object, you need to handle this event. 
            MyBase.PerformDataBinding(OnMustAddARow(data))
            Exit Sub
        End If
    End Sub

    Protected Function OnMustAddARow(ByVal data As IEnumerable) As IEnumerable
        If MustAddARow Is Nothing Then
            Throw New NullReferenceException("The datasource has no rows. You must handle the ""MustAddARow"" Event.")
        End If
        Return MustAddARow(data)
    End Function

    'Public Event MustAddARow As MustAddARowHandler
End Class
